<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$member=$this->model('member')->checkmember('',true);
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$type=$_RQ['type'];
	$feetype=$this->setting['bufee_type']?:3;
	if($type){
		$return['feetitle']=$this->credit_type[$feetype];
		$return['credit_rate']=$paysetting['credit'.$feetype.'_rate']?:1;
	}
	$return['item']=$this->model('member')->getdetail();
	$return['item']['fee']=$type?$return['item']['credit'.$this->setting['bufee_type']]:$return['item']['credit2'];
	$return['item']['type']=$type;
	$paysetting=uni_setting('pay');
	exi($return);
}
if($_RQ['op']=='recharge'){
	$post=get_postdata();
	if(intval($post['money'])<=0){
		exi('请先设置充值金额！并且为整数！','error');
	}
	$fee=sprintf("%.2f", intval($post['money']));
	$dian_mid=$member['daili_id'];
	$agent=$this->model('member')->getdetail($dian_mid);
	if($post['type']){
		$feetype=$this->setting['bufee_type'];
		$feetitle=$this->credit_type[$this->setting['bufee_type']];
		if($dian_mid && $agent['credit'.$this->setting['bufee_type']]<$post['money']){
			exi('上级经销商'.$feetitle.'不足！无法充值！','error');
		}
		$paysetting=uni_setting('pay');
		$credit_rate=$paysetting['credit'.$feetype.'_rate']?:1;
	}else{
		$feetype=2;
		$feetitle='余额';
		if($dian_mid && $agent['credit2']<$post['money']){
			exi('上级经销商余额不足！无法充值！','error');
		}
		$credit_rate=1;
	}
	$money=$fee/$credit_rate;
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'msg'=>$feetitle.'充值',
		'feetype'=>$feetype,
		'type'=>0,
		'fee'=>$fee,
		'money'=>$money,
		'status'=>0,
		'pay_id'=>0,
		'dian_mid'=>$dian_mid,
		'createtime'=>TIMESTAMP
	);
	pdo_insert('jy_area_fee_feelog',$data);
	$id=pdo_insertid();
	$params=array(
		'id'=>$id,
		'fee'=>$money,
		'title'=>$feetitle.'充值',
		'plugin'=>'jy_area_fee',
		'pay_type'=>'wechat',
		'no_type'=>array('hdfk'),
		'back_url'=>'jy_area_fee/recharge/pay_result/',
		'success_url'=>'jy_area_fee/feelog/',
		'error_url'=>'jy_area_fee/feelog/',
	);
	$pay_id=cfc('pay')->create_pay_order($params);
	pdo_update('jy_area_fee_feelog',array('pay_id'=>$pay_id),array('id'=>$id));
	exi($pay_id);
}
if($_RQ['op']=='pay_result'){
	$payresult=$_SESSION['pay_result'];
	$recharge=pdo_get('jy_area_fee_feelog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if($recharge['money']!=$payresult['fee']){
		exi('支付失败，支付金额不匹配！','error');
	}
	if($recharge['status']){
		exi('该订单已支付！无需重复支付！','error');
	}
	if($payresult['status']!=1){
		exi('充值失败！','error');
	}
	pdo_update('jy_area_fee_feelog',array('status'=>1),array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	model('core/member')->member_credit($_SESSION['uid'],'credit'.$recharge['feetype'],$recharge['fee'],true,'jy_area_fee','充值');
	if($recharge['dian_mid']){
		model('core/member')->member_credit($recharge['dian_mid'],'credit'.$recharge['feetype'],$recharge['fee'],false,'jy_area_fee','下级充值');
	}
	exi('充值成功！');
}